package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.RoleDTO;
import com.cskaoyan.wordmemorize.dto.admin.RolePermissionDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.RoleCommand;
import com.cskaoyan.wordmemorize.request.RolePermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RolePermissionController {

    //    -----------------------------------------------------------------------------
//    叶佳杰：
    @Autowired
    RolePermissionService rolePermissionService;

    //    查询列表部分：getRoleList
    @GetMapping("admin/role/list")
    @SaAdminCheckPermission("admin:role:search")
    public Result<PageDTO<RoleDTO>> roleList(@ModelAttribute PageRequest pageRequest) {
        return Result.ok(rolePermissionService.getRoleList(pageRequest));
    }

    //    添加管理员部分：addRole
    @PostMapping("/admin/role")
    @SaAdminCheckPermission("admin:role:add")
    public Result<String> insertRole(@RequestBody RoleCommand roleCommand) {
        if (roleCommand.getName() != null || roleCommand.getName() != "") {
            rolePermissionService.addRole(roleCommand);
            return Result.ok("");
        }
        return Result.fail();
    }

//    修改管理员名称部分：updateRole
    @PutMapping("/admin/role")
    @SaAdminCheckPermission("admin:role:update")
    public Result<String> changeRoleName(@RequestBody RoleCommand roleCommand) {
        if (roleCommand.getName() != null || roleCommand.getName() != "") {
            rolePermissionService.updateRole(roleCommand);
            return Result.ok("");
        }
        return Result.fail();
    }

//    更新角色权限关系：updateRolePermission
    @PutMapping("/admin/role/permission")
    @SaAdminCheckPermission("admin:role:authorize")
    public Result<String> changeRolePermission(@RequestBody RolePermissionCommand command) {
        if (command.getRoleId()!=null||command.getRoleId()!="") {
            rolePermissionService.updateRolePermission(command);
            return Result.ok("");
        }
        return Result.fail();
    }

//    获取角色权限树：getRolePermissionList
    @GetMapping("/admin/{roleId}/permission/list")
    @SaAdminCheckPermission("admin:permission:search")
    public Result<RolePermissionDTO> rolePermissionList(@PathVariable("roleId") Long roleId) {
        return Result.ok(rolePermissionService.getRolePermissionList(roleId));
    }

//    删除账号角色：deleteRole
    @DeleteMapping("/admin/role/{roleId}")
    @SaAdminCheckPermission("admin:role:delete")
    public Result<DeleteRoleOperateDTO> deleteRoleInfo(@PathVariable("roleId") Long roleId){
        return Result.ok(rolePermissionService.deleteRole(roleId));
    }

//    删除角色:deleteRolePermission
    @DeleteMapping("/admin/permission/{roleId}/{permissionId}")
    @SaAdminCheckPermission("admin:permission:delete")
    public  Result<String> deleteRolePermissionInfo(@PathVariable("roleId")Long roleId, @PathVariable("permissionId") List<Long> permissionId){
        rolePermissionService.deleteRolePermission(roleId,permissionId);
        return Result.ok("");
    }



}
